<?php
class libraryActions extends BaseActions
{
    public  $m;
    public function __construct()
    {
        parent::__construct();
        Files::factory('Questions');
		$this->m = new Questions();
    }
    /**
    *
    * 题库首页
    */
    public function actionsIndex()
    {
    }

    /**
    *
    * 添加题库
    */
    public function actionsAdd()
    {
    	if (Http::post()){
    	    
    		$arr = array(
    		'type'=>Http::post('type'),
    		'cate'=>Http::post('cate'),
    		'question'=>Http::post('question'),
    		'answer'=>Http::post('answer'),
    		'ctime'=>date("Y-m-d H:i:s"),
    		'cby'=>user('uid'),
    		'status'=>1,
    		'utime'=>date("Y-m-d H:i:s"),
    		'uby'=>user('uid'));
    		if (Http::get('do')=='update'){
    		    $id = Http::get('id');
    		    unset($arr['ctime']);
    		    unset($arr['cby']);
    		    if($this->m->db->updateData('q_lib', $arr, " WHERE id = '$id'")){
    		        echo 'update succuss';
        		}else {
        			echo 'sys error';
        		}
    		}else {
        		if($this->m->db->insert('q_lib', $arr)){
        			echo 'insert succuss';
        		}else {
        			echo 'sys error';
        		}
    		}
    		die();
    	}
    	$row = array();
    	
    	if (Http::get('id')){
    		$row = $this->m->getQuestionById(Http::get('id'));
    	}
        $this->addJs(MVC_WWW_HOST.'js/jquery/dragdrop.js');
        $types = $this->m->getAllType();
        $cates = $this->m->getCateByUser(user('uid'));
        $this->assign('row', $row);
        $this->assign('types', $types);
        $this->assign('cates', $cates);
    }
    public function actionsAnswer()
    {
    	$cate = Http::get('catehtml');
    	$row = array();
    	$answer = array();
    	if (Http::get('id')){
    		$row = $this->m->getQuestionById(Http::get('id'));
    		if ($cate==$row['type']){
        		$answer = json_decode($row['answer'],true);
        	    $row['answer'] = $answer;
    		}
    	}    	
    	$this->assign('ans', $answer);  
    	$this->assign('row', $row);
    	$this->display('answer'.$cate.'.tpl.php');
    }
    public function actionsQuestions()
    {
    	$types = $this->m->getAllType();
    	$ques  = $this->m->getAllQuestions();
        $this->assign('types', $types);
        $this->assign('ques', $ques);
    }
    public function actionsSimple()
    {
    	$types = $this->m->getAllType();
    	$ques  = $this->m->getAllQuestions();
        $this->assign('types', $types);
        $this->assign('ques', $ques);
    }
    public function actionsGetques()
    {
    	//echo Http::get('id');
    	if (!key_exists('pc'.Http::get('id'), $_SESSION)){
    		$_SESSION['pc'.Http::get('id')]=1;
    	}
    	$ques  = $this->m->getQuesTitle(Http::get('items'));
    	$html = '';
    	foreach ($ques as $k=>$v){
    		$html = $html.'<div class="ques_divc"  id="c_'.Http::get('id').'_'.$v['id']. '">'
    		.'<span class="ques_spanc">&nbsp;<input type="text" class="input8" name="pc_sort_'.Http::get('id').'_'.$v['id']. '" value="'.Http::get('id').'.'.$_SESSION['pc'.Http::get('id')].'">'
    		.'</span>'
    		.'<span class="ques_ques">&nbsp;'.$v['question'].'</span>'
    		.'<span class="ques_point">分数：<input type="text" class="input8" name="pc_score_'.Http::get('id').'_'.$v['id']. '" ></span>'
    		.'<span>'
    		.'<button type="button" class="search_submit2" onclick="delQues('."'c_" .Http::get('id').'_'.$v['id']."'". ',0)">删除 </button></span></div>';
    		$_SESSION['pc'.Http::get('id')]++;
    	}
    	echo $html;
    	die();
    }
    public function actionsAddcate(){
    	$arr = array('rs'=>0,'html'=>'添加失败');
    	if (Http::get()){    		
    		$name = Http::get('savecate');
    		$uid = user('uid');
    		if ($name){
    			$data = array('name'=>$name,'uid'=>$uid,'islast'=>1);   
        		if ($this->m->getCateByUserAndName($uid,$name)){
        			$arr = array('rs'=>0,'html'=>'添加失败，重复添加');
        		}else {
        			$this->m->db->updateData('q_cate', array('islast'=>0), " WHERE uid = '$uid'  ");
    	    		$id = $this->m->db->insert('q_cate', $data,true);	    		
    	    		if ($id){
    	    			$arr = array('rs'=>1,'html'=>'添加成功','id'=>$id);
    	    		}
    	    	} 			
    		} else{
    		    $arr = array('rs'=>0,'html'=>'分类名称不能为空');
    		}   		
    		
    	}    	
    	echo json_encode($arr);
    	die();
    }
    public function actionsDel(){
    	$id = Http::get('id');
    	if ($id){
    		$sql = "UPDATE `q_lib` SET `status` = 0 WHERE `id` IN ( $id )";
    		if($this->m->db->delete($sql)){
    			echo '删除成功！';
    		}
    	}
    	die();
    	
    }
}#classEnd